<template>
  <el-button type="default"
             :icon="icon"
             @click="submit">
    导出
  </el-button>
</template>

<script>
import { table } from "@/api";
import { mapState, mapActions } from "vuex";
export default {
  name: "ExportExcel",
  props: {
    filter: Object,
    transform: {
      type: Object,
      default: () => { }
    },
    url: {
      type: String,
      default: ""
    },
    icon: {
      type: String,
      default: "el-icon-s-promotion"
    }
  },
  data () {
    return {
      loading: false,
      userInfo: sessionStorage.getItem('userInfo')
    };
  },
  computed: {
    ...mapState({
      menu: (state) => state.menu.menu,
      records: (state) => state.table.records
    }),
    user () {
      return JSON.parse(this.userInfo)
    }
  },
  methods: {
    submit () {
      if (!this.transform.exportType || !this.records.length) {
        this.$message.warning("没有可要导出的数据！");
        return false;
      }
      this.loading = true
      let filter = JSON.parse(JSON.stringify(this.filter));
      const { ...searchFilter } = filter;
      const { exportType, name, transformFilter } = this.transform;

      // for (let ele in searchFilter) {
      //   if (ele.includes("Time") || ele.includes("Date")) {
      //     searchFilter[`${ele}Begin`] = this.$moment(searchFilter[ele][0]).format("YYYY-MM-DD") + " 00:00:00";
      //     searchFilter[`${ele}End`] = this.$moment(searchFilter[ele][1]).format("YYYY-MM-DD") + " 23:59:59";
      //   }
      //   if (ele.includes("total") || ele.includes("calculate")) {
      //     searchFilter[`${ele}Begin`] = searchFilter[ele].min ? Number(searchFilter[ele].min) : null;
      //     searchFilter[`${ele}End`] = searchFilter[ele].max ? Number(searchFilter[ele].max) : null;
      //   }
      // }
      let params = {
        args: { ...transformFilter, ...searchFilter, tenantId: this.user?.tenantId },
        name: `${name}${this.$moment(new Date()).format("YYYYMMDDhhmmss")}`,
        type: exportType
      };
      this.exportConfigApi(this.url, params);
    },
    // 导出配置公共接口
    async exportConfigApi (url, params) {
      await table.submitFormContainer(url, params);
      this.searchCount() //刷新导出未读数
      this.$nextTick(() => {
        this.$message.success("导出功能正在后台执行中，请稍后去“导出文件”下载中查看！");
        this.loading = false
      });
    },
    ...mapActions({
      searchCount: "login/exportCount"
    }),
  }
};
</script>
<style lang="scss"></style>
